from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import NoSuchElementException
import getpass


# DOWNLOAD_PATH = r'C:\Users\{}\Downloads'.format(getpass.getuser())
# tmp_path = DOWNLOAD_PATH + '\\dist\\'
# options = Options()
# options.add_experimental_option('debuggerAddress', '127.0.0.1:9201')
# prefs = {'profile.default_content_settings.popups': 0,  # 防止保存弹窗
#          'download.default_directory': tmp_path,  # 设置默认下载路径
#          "profile.default_content_setting_values.automatic_downloads": 1  # 允许多文件下载
#          }
# prefs = {'download.default_directory': tmp_path,  # 设置默认下载路径
#          }
# options.add_experimental_option('prefs', prefs)
# options.add_experimental_option('excludeSwitches', ['enable-automation'])
# options.add_argument('''--disable-gpu''')
# options.add_argument('''--no-sandbox''')
# options.add_argument(
#     '--user-agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36"')
# driver = webdriver.Chrome(options=options)
# driver.command_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')
# params = {'cmd': 'Page.setDownloadBehavior', 'params': {'behavior': 'allow', 'downloadPath': DOWNLOAD_PATH + '/dist'}}
# command_result = driver.execute("send_command", params)

#用于定义下载不弹窗和默认下载地址（默认下载地址还要再后面的commands里启动，默认是不开启的）
chromeOptions = webdriver.ChromeOptions()
prefs = {"download.default_directory": "D:\\dist\\"}
chromeOptions.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(options=chromeOptions)